// Declare worker scripts path for zip manipulation
zip.workerScriptsPath = 'static/js/';

// Declare layer
vector = new ol.layer.Vector({
    source: new ol.source.Vector({
        format: new ol.format.KML({
            extractStyles: true
        })
    })
});

var googleMapLayer = new ol.layer.Tile({
    source: new ol.source.XYZ({
        coorOffset: {X: 605, Y: 443},
        url: 'http://www.google.cn/maps/vt/pb=!1m4!1m3!1i{z}!2i{x}!3i{y}!2m3!1e0!2sm!3i380072576!3m8!2szh-CN!3scn!5e1105!12m4!1e68!2m2!1sset!2sRoadmap!4e0!5m1!1e0'  
        //url: 'http://mt3.google.cn/maps/vt?lyrs=s%40794&hl=zh-CN&gl=CN&x={x}&y={y}&z={z}'
    })

});



var map = new ol.Map({
    layers: [googleMapLayer],
    view: new ol.View({
        center: [-122.0838851,37.422104808],//[112.9324919220994, 28.16391954413889],
        projection: 'EPSG:4326', // 'EPSG:3857',// 
        zoom: 16
    }),
    target: 'map'
});

// Url to KMZ file (in fact, it's a kml zipped file and not a gzipped file)
var url = '../../kmz/test.kmz';
// var url = '/proxy/www.spc.noaa.gov/products/watch/ActiveWW.kmz';

// Function to ease KML feature reading
function addFeatures(text) {
    var formatter = new ol.format.KML();
    var kml_features = formatter.readFeatures(text, {
        dataProjection: 'EPSG:4326',
        featureProjection: 'EPSG:3857'
    });
    vector.getSource().addFeatures(kml_features);
}

// Function to parse KML text to get link reference to other KMZ
function parseKmlText(text) {
    var oParser = new DOMParser();
    var oDOM = oParser.parseFromString(text, 'text/xml');
    var links = oDOM.querySelectorAll('NetworkLink Link href');
    var files = Array.prototype.slice.call(links).map(function (el) {
        return el.textContent;
    });
    // console.log(files);
    return files;
}

// Function to unzip content from blob and execute callback on
// first entry (not generic but assumed for the demo)
function unzipFromBlob(callback) {
    return function unzip(blob) {
        // use a BlobReader to read the zip from a Blob object
        zip.createReader(new zip.BlobReader(blob), function (reader) {
            // get all entries from the zip
            reader.getEntries(function (entries) {
                if (entries.length) {
                    // get first entry content as text
                    entries[0].getData(new zip.TextWriter(), function (text) {
                        // text contains the entry data as a String
                        console.log(text);
                        callback(text);
                        // close the zip reader
                        reader.close(function () {
                            // onclose callback
                        });

                    }, function (current, total) {
                        // onprogress callback
                    });
                }
            });
        }, function (error) {
            // onerror callback
        });
    };
}

// Function to make ajax call and make a callback on success
function ajaxKMZ(url, callback) {
    qwest.get(url, null, {
        responseType: 'blob'
    })
            .then(function (response) {
                // Run when the request is successful
                callback(response);
            })
            .catch(function (e, url) {
                // Process the error
            })
            .complete(function () {
                // Always run
            });
}

// Read reference to other KMZ and add them to the vector layer
var readAndAddFeatures = function (text) {
    var listFilesKMZ = parseKmlText(text);
    console.log(listFilesKMZ);
    listFilesKMZ.forEach(function (el) {
        // console.log(el);
        // Nested calls. Acceptable for a demo
        // but could be "promisified" instead
        ajaxKMZ(el, unzipFromBlob(addFeatures));
    });
};


function repeat_kmz_calls() {
    var combinedCallback = unzipFromBlob(readAndAddFeatures);
    // make the ajax call to kmz that unzip and read the file
    // this file reference other KMZ so we call each of them
    // and add their content
    ajaxKMZ(url, combinedCallback);
    setTimeout(repeat_kmz_calls, 60000);
}
vector.on('render', function (event) {
    var ctx = event.context;
    ctx.fillStyle = "red";
    ctx.font = "72px Arial";
    // get the metrics with font settings
    var metrics = ctx.measureText("WaterMark Demo");
    var width = metrics.width;

    if (vector.getSource().getFeatures().length == 0) {
        ctx.fillText("WaterMark Demo", ctx.canvas.width / 2 - (metrics.width / 2), ctx.canvas.height / 2);
    }
    ctx.restore();
});

repeat_kmz_calls();


